<?php include Doo::conf()->SITE_PATH .  Doo::conf()->PROTECTED_FOLDER . "viewc//menu.php"; ?>

<div id='main-site-globalbo-moduleSpace-flat'>
  <div id='main-site-globalbo-moduleSpace-flat-search'>
  Immeuble : 
    <select id='immeuble' name='immeuble'>
      <option value='none'></option>
      <?php foreach($data['immeubles'] as $k1=>$v1): ?>
        <option value="<?php echo $v1->imm_id; ?>""><?php echo $v1->imm_nom; ?></option>
      <?php endforeach; ?>
    </select>    
    <div id='component-add-flat'><img src='<?php echo $data['baseurl']; ?>global/img/globalbo/appartements/add.png' alt='Ajouter Appartement'> Ajouter Appartement </div>
  </div>
  
  <div id='main-site-globalbo-moduleSpace-flat-result'>
    <ul>
      <?php foreach($data['allApparts'] as $k1=>$v1): ?>
        <li id="<?php echo $v1->cha_id; ?>"> 
          <span class='main-site-globalbo-moduleSpace-flat-result-immeuble'><?php echo $v1->FrbFlatImmeuble->imm_nom; ?></span>
          <span class='main-site-globalbo-moduleSpace-flat-result-chambre'><?php echo $v1->cha_num; ?></span>
          <span style='display:none' class='main-site-globalbo-moduleSpace-flat-result-chambre-lowloy'><?php echo $v1->cha_loy_low; ?></span>
          <span style='display:none' class='main-site-globalbo-moduleSpace-flat-result-chambre-highloy'><?php echo $v1->cha_loy_high; ?></span>
          <span style='display:none' class='main-site-globalbo-moduleSpace-flat-result-chambre-surface'><?php echo $v1->cha_surf; ?></span>
          <span style='display:none' class='main-site-globalbo-moduleSpace-flat-result-chambre-desc'><?php echo $v1->cha_det; ?></span>
          
          <span style='display:none' class='main-site-globalbo-moduleSpace-flat-result-ville'><?php echo $v1->FrbFlatImmeuble->FrbFlatVille->vil_nom; ?></span>        
        </li>
      <?php endforeach; ?>
    </ul>
  </div>
  
  <div id='main-site-globalbo-moduleSpace-flat-detail'>
    <div id='main-site-globalbo-moduleSpace-flat-detail-photo'></div>
    <h2> <span id='main-site-globalbo-moduleSpace-flat-detail-immeuble'> </span> - <span id='main-site-globalbo-moduleSpace-flat-detail-chambre'> </span></h2>
    Loyer Bas : <span id='main-site-globalbo-moduleSpace-flat-detail-lowLoy'> </span>
    <!-- Loyer haut :  --><span id='main-site-globalbo-moduleSpace-flat-detail-highLoy'> </span>
    Surface : <span id='main-site-globalbo-moduleSpace-flat-detail-surface'> </span>m²
    <p>Description :</p><p> <span id='main-site-globalbo-moduleSpace-flat-detail-description'> </span></p>
        
    <input type='hidden' id='appart_id_modif' name='appart_id_modif' value=''>
    <input type='button' value='Modifier' id='main-site-globalbo-moduleSpace-flat-detail-modifier' class='main-site-globalbo-moduleSpace-flat-detail-modif personal-button'>
    <input type='button' value="Gérer l'occupation" id='main-site-globalbo-moduleSpace-flat-detail-reservation' class='personal-button'>
    
    <div id='main-site-globalbo-moduleSpace-flat-detail-pictures'></div>
    <input type='button' value='Ajouter photo' id='main-site-globalbo-moduleSpace-flat-detail-addPictures' class='personal-button'>
  </div>
</div>

<div id='componen-add-flat-screen' class='popup-screen'>
  <div id='componen-add-flat-screen-box' class='popup-screen-box'>
    <span id='componen-add-flat-screen-box-close' class='popup-screen-box-close'>fermer X</span>    
    <div id='componen-add-flat-screen-box-title' class='popup-screen-box-title'>Appartements</div>
    
    <?php echo $data['formFlatAdmin']; ?>
  </div>
</div>

<div id='component-change-reservation' class='popup-screen'>
  <div id='component-change-reservation-box' class='popup-screen-box'>
  <span id='component-change-reservation-close' class='popup-screen-box-close'>fermer X</span>
  <div id='component-change-reservation-title' class='popup-screen-box-title'>Gestion de l'occupation de l'appartement</div>
  <div id='component-change-reservation-tuple'>
    
  </div>
  <div id='component-change-reservation-add'>Ajouter réservation : 
  <input type='button' id='component-change-reservation-reset' value='Nouveau' class='personal-button'></div>
  <?php echo $data['formReservationAdmin']; ?>
  </div>
</div>

<div id='component-show-image' class='popup-screen'>
  <div id='component-show-image-box' class='popup-screen-box'>
    <span id='component-show-image-close' class='popup-screen-box-close'>fermer X</span>
    <img id='component-show-image-img' src='' alt='Image grande taille'>
  </div>
</div>

<div id='component-add-image' class='popup-screen'>
  <div id='component-add-image-box' class='popup-screen-box'>
    <span id='component-add-image-close' class='popup-screen-box-close'>fermer X</span>
    <div id='component-add-image-title' class='popup-screen-box-title'>Ajouter photo</div>
    <iframe id='component-add-image-box-iframe' src='' width='400px' height='300px' style='border : none; text-align: center;'>
    
    </iframe>
  </div>
</div>
<script type="text/javascript">

function filterSearch(filter_data){
  url="<?php echo $data['baseurl']; ?>admin/frbFlat/frbFlat_MainController/appartements";
  $("#main-site-globalbo-moduleSpace-flat-detail").fadeOut('slow', function(){
    $("#main-site-globalbo-moduleSpace-flat-result").html("");
      $.ajax({
        urlApp: url,
        type: 'POST',
        cache: false,
        data: filter_data,
        success: function(html){
        
          $('#main-site-globalbo-moduleSpace-flat-result').html(html);
          $('#main-site-globalbo-moduleSpace-flat-result').fadeIn('slow');
        }
      });
  });  
}

/** Submit add/change Flat**/
function submitFlat(){
  flatData = $('#componen-add-flat-screen-box-form').serialize();
  $('#componen-add-flat-screen-box-form').fadeOut('slow');
  $('#componen-add-flat-screen-box').css('background', "white url('<?php echo $data['baseurl']; ?>global/img/globalbo/appartements/ajax-loader.gif') center no-repeat");
  url="<?php echo $data['baseurl']; ?>admin/frbFlat/frbFlat_MainController/appartements";
  $.ajax({
    urlApp: url,
    type: 'POST',
    cache: false,
    data: flatData,
    success: function(html){
    $('#componen-add-flat-screen-box').css('background', 'white');
    if(html == "ok"){
      alert("Enregistrement effectué");
    }else{
      alert("Erreur lors de l'enregistrement");
    }
    $('#componen-add-flat-screen-box-form').fadeIn('slow');
    /*//Fake data pour forcer le rechargement des apparts
    fakeData = "immeuble=none";
    filterSearch(fakeData);*/
    }
  });  
  
  return false;
}

/** Gestion de la réservation - Submit form **/
function submitReserv(){
  var datedeb = $("#component-change-reservation-form-datedeb").val();
  var datefin = $("#component-change-reservation-form-datefin").val();
  //alert(datedeb+datefin);
  /*if(datedeb>=datefin){
    alert('ERREUR - Date de début plus grande que date de fin');
    return false;
  }*/
  data_reservation = $("#component-change-reservation-form").serialize();
  url="<?php echo $data['baseurl']; ?>admin/frbFlat/frbFlat_MainController/appartements";
  $.ajax({
    urlApp: url,
    type: 'POST',
    cache: false,
    data: data_reservation,
    success: function(html){
      if(html!=""){
        alert(html);
        //document.write(html);
        return false;
      }  
      id_chambre = $('#appart_id_modif').val().replace('appart_', '');
      loadReservation(id_chambre);
    }
  });  
  return false;
}

function loadReservation(id_chambre){
  url="<?php echo $data['baseurl']; ?>admin/frbFlat/frbFlat_MainController/appartements";
  $.ajax({
    urlApp: url,
    type: 'POST',
    cache: false,
    data: "get_reservation=true&id="+id_chambre,
    success: function(html){
      //alert(html);
      $("#component-change-reservation-tuple").html(html);
    }
  });  
}
/** Load pictures for flat **/
function loadPictures(id_flat){
  url="<?php echo $data['baseurl']; ?>admin/frbFlat/frbFlat_MainController/appartements";
  $.ajax({
    urlApp: url,
    type: 'POST',
    cache: false,
    data: "get_pictures=true&id_flat="+id_flat,
    success: function(html){
      //alert(html);
      $("#main-site-globalbo-moduleSpace-flat-detail-pictures").html(html);
    }
  });  
}

function emptyReservation(){
  $('#component-change-reservation-form-chambreres').val("");
  $('#component-change-reservation-form-indicator').val("");
  $('#component-change-reservation-form-datedeb').val("");
  $('#component-change-reservation-form-datefin').val("");
  $('#component-change-reservation-form-locataire').val("");
  $('#component-change-reservation-form-prenom').val("");
  $('#component-change-reservation-form-daten').val("");
  $('#component-change-reservation-form-lieun').val("");
  $('#component-change-reservation-form-date_entree').val("");
  $('#component-change-reservation-form-dept').val("");
  $('#component-change-reservation-form-adresse').val("");
  $('#component-change-reservation-form-ville').val("");
  //alert('done');
}

/** Main **/
$('#main-site-globalbo-moduleSpace-flat-detail').hide();
$('.popup-screen').hide();
$(document).ready(function(){

  /** Type only date key valid **/
  //$(".type-date").keydown(function(event){ typeDate(event, $(this)); });
  
  /** Open de la box add image **/
  $('#main-site-globalbo-moduleSpace-flat-detail-addPictures').click(function(){
    var id_chambre = $('#appart_id_modif').val().replace('appart_', '');
    $('#component-add-image-box-iframe').attr('src', '<?php echo $data['baseurl']; ?>syndication_up?id='+id_chambre);
    $('#component-add-image').show();
  });
  
  /** Close de la box add image **/
  $('#component-add-image-close').click(function(){
    var id_chambre = $('#appart_id_modif').val().replace('appart_', '');
    loadPictures(id_chambre);
    $('#component-add-image').hide();
  });

  /**Close de la box show image **/
  $('#component-show-image-close').click(function(){
    $('#component-show-image').hide();
  });

  /** Gestion des datePicker **/
  $('.calendar').datepicker();
  
  /** Change immeuble search filter **/
  $("#immeuble").change(function(){
    filter_data = $(this).serialize();
    filterSearch(filter_data);
    
  });

  /** Open Change reservation popup **/
  $('#main-site-globalbo-moduleSpace-flat-detail-reservation').click(function(){
    var id_chambre = $('#appart_id_modif').val().replace('appart_', '');
    $('#component-change-reservation').show();
    $('#component-change-reservation-form-chambre').val(id_chambre);
    $('#component-change-reservation-form-indicator').val("new");
    loadReservation(id_chambre);
  });

  /** Close reservation popup **/
  $('#component-change-reservation-close').click(function(){
    $('#component-change-reservation').hide();
    emptyReservation();
  });

  

  /** Vider reservation popup **/  
  $('#component-change-reservation-reset').click(function(){
    emptyReservation();    
    $('#component-change-reservation-form-indicator').val("new");
  });
  

  /** Change Flat information**/
  $('#main-site-globalbo-moduleSpace-flat-detail-modifier').click(function(){
    var name = $('#main-site-globalbo-moduleSpace-flat-detail-chambre').html();
    var loyer = $('#main-site-globalbo-moduleSpace-flat-detail-lowLoy').html();
    var surface = $('#main-site-globalbo-moduleSpace-flat-detail-surface').html();
    var detail = $('#main-site-globalbo-moduleSpace-flat-detail-description').html();
    var immeuble = $('#main-site-globalbo-moduleSpace-flat-detail-immeuble').html();
    var id_flat = $('#appart_id_modif').val().replace('appart_', '');
    
    $("#componen-add-flat-screen-box-form-name").val(name);
    $("#componen-add-flat-screen-box-form-loyer").val(loyer);
    $("#componen-add-flat-screen-box-form-surface").val(surface);
    $("#componen-add-flat-screen-box-form-detail").val(detail);
    $("#componen-add-flat-screen-box-form-id").val(id_flat);
    $("#componen-add-flat-screen-box-form-immeuble option").each(function(){
      if($(this).text() == immeuble){
        $(this).attr('selected', 'selected');
      }
    });
    $('#action-indicator').attr('value', 'change');
    $('#componen-add-flat-screen').show();
  });

  /** Add Flat from button**/
  $('#component-add-flat').click(function(){
    $("#componen-add-flat-screen-box-form-name").val("");
    $("#componen-add-flat-screen-box-form-loyer").val("");
    $("#componen-add-flat-screen-box-form-surface").val("");
    $("#componen-add-flat-screen-box-form-detail").val("");
    $("#componen-add-flat-screen-box-form-immeuble option").each(function(){
        $(this).removeAttr('selected');
    });
    $('#action-indicator').attr('value', 'new');
    $('#componen-add-flat-screen').show()
  });

  /** Close add/change Flat box **/
  $('#componen-add-flat-screen-box-close').click(function(){
    $('#action-indicator').attr('value', '');
    $('#appart_id_modif').val('');
    //Fake data pour forcer le rechargement des apparts
    fakeData = "immeuble=none";
    filterSearch(fakeData);
    $('#componen-add-flat-screen').hide()
  });

  /**  affiche le détail d'un appartement via la li**/
  $('#main-site-globalbo-moduleSpace-flat-result li').click(function(){
    thisClick = $(this);
    $('.result-activeli').attr('class', '');
    thisClick.attr('class', 'result-activeli');
    $('#main-site-globalbo-moduleSpace-flat-detail').fadeOut('slow', function(){
      var ville = thisClick.children('main-site-globalbo-moduleSpace-flat-result-ville').html();
      var immeuble = thisClick.children('.main-site-globalbo-moduleSpace-flat-result-immeuble').html();
      var appart = thisClick.children('.main-site-globalbo-moduleSpace-flat-result-chambre').html();
      var idappart = thisClick.attr('id');
      var low_loy = thisClick.children('.main-site-globalbo-moduleSpace-flat-result-chambre-lowloy').html();
      //var high_loy = thisClick.children('.main-site-globalbo-moduleSpace-flat-result-chambre-highloy').html();
      var description = thisClick.children('.main-site-globalbo-moduleSpace-flat-result-chambre-desc').html();
      var surface = thisClick.children('.main-site-globalbo-moduleSpace-flat-result-chambre-surface').html();;

      $('#main-site-globalbo-moduleSpace-flat-detail #main-site-globalbo-moduleSpace-flat-detail-immeuble').html(immeuble);
      $('#main-site-globalbo-moduleSpace-flat-detail #main-site-globalbo-moduleSpace-flat-detail-chambre').html(appart);
      $('#main-site-globalbo-moduleSpace-flat-detail #main-site-globalbo-moduleSpace-flat-detail-lowLoy').html(low_loy);
      $//('#main-site-globalbo-moduleSpace-flat-detail #main-site-globalbo-moduleSpace-flat-detail-highLoy').html(high_loy);
      $('#main-site-globalbo-moduleSpace-flat-detail #main-site-globalbo-moduleSpace-flat-detail-surface').html(surface);
      $('#main-site-globalbo-moduleSpace-flat-detail #main-site-globalbo-moduleSpace-flat-detail-description').html(description);
      $('#main-site-globalbo-moduleSpace-flat-detail .main-site-globalbo-moduleSpace-flat-detail-modif').attr('id', 'appart_'+idappart);
      $('#main-site-globalbo-moduleSpace-flat-detail #appart_id_modif').val('appart_'+idappart);
      loadPictures(idappart);
    });
    $('#main-site-globalbo-moduleSpace-flat-detail').fadeIn('slow');
  });
});
</script>